unit uFrmCageCashGame;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, gnugettext, Buttons, UfrmSellTicket,ufrmcanceloperation, usqls, umainutils, ExtCtrls, uutils, ufrmcashgameplayerout;

type
  TfrmCageCashGame = class(TForm)
    stg_CashGames: TStringGrid;
    Label13: TLabel;
    lbl_CashGameName: TLabel;
    stg_Players: TStringGrid;
    Label4: TLabel;
    Label1: TLabel;
    btn_PlayerOut: TSpeedButton;
    img_Player: TImage;
    lbl_OutColor: TLabel;
    lbl_RebColor: TLabel;
    btn_CancelOperation: TSpeedButton;
    procedure stg_CashGamesDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure stg_PlayersDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure Button1Click(Sender: TObject);
    procedure stg_CashGamesSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    procedure stg_CashGamesDblClick(Sender: TObject);
    procedure btn_PlayerOutClick(Sender: TObject);
    procedure stg_PlayersSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    procedure stg_PlayersDblClick(Sender: TObject);
    procedure btn_CancelOperationClick(Sender: TObject);
  private
    { Private declarations }
    LastCashGameSelRow: Integer;
    LastPlayerUpdateQuery: string;
    CanSelect: Boolean;
  public
    procedure GetCashGameData();
    { Public declarations }
  end;

var
  frmCageCashGame: TfrmCageCashGame;

implementation

uses ufrmmain;
{$R *.dfm}

procedure TfrmCageCashGame.btn_CancelOperationClick(Sender: TObject);
begin
  if (stg_Players.Row > 1) and (stg_Players.Cells[0, stg_Players.Row] <> '') then
  begin
    LastCashGameSelRow := stg_CashGames.Row;
    FrmCancelOperation.ShowForm(stg_Players.Cells[0, stg_Players.Row], stg_Players.Cells[1, stg_Players.Row], false, true);
    stg_CashGamesSelectCell(self, 0, LastCashGameSelRow, CanSelect);
  end;
end;

procedure TfrmCageCashGame.btn_PlayerOutClick(Sender: TObject);
begin
  if MessageDlg(_('Player Out?'), mtWarning, [mbYes, mbNo], 0) = mrYes then
  begin
    LastCashGameSelRow := stg_CashGames.Row;
    LastPlayerUpdateQuery := 'ID=' + stg_Players.Cells[0, stg_Players.Row];
    FrmCashGamePlayerOut.ShowForm(stg_Players.Cells[0, stg_Players.Row]);
    stg_CashGamesSelectCell(self, 0, LastCashGameSelRow, CanSelect);
  end;
end;

procedure TfrmCageCashGame.Button1Click(Sender: TObject);
begin
  GetCashGameData;
end;

procedure TfrmCageCashGame.GetCashGameData;
var
  SelRow: Integer;
begin
  lbl_CashGameName.Caption := '';
  SelRow := stg_CashGames.Row;
  frmMain.GetGridData(stg_CashGames, SQL_CageCashGame, false);
  stg_CashGames.ColWidths[0] := -1;
  stg_CashGames.ColWidths[1] := 80;
  stg_CashGames.ColWidths[2] := 150;
  stg_CashGames.ColWidths[3] := 70;
  stg_CashGames.ColWidths[4] := 60;
  stg_CashGames.ColWidths[5] := 70;
  stg_CashGames.ColWidths[6] := 80;
  stg_CashGames.ColWidths[7] := 80;
  stg_CashGames.ColWidths[8] := 80;
  if SelRow < 2 then
    SelRow := 2;
  frmMain.SelectGridRow(stg_CashGames, SelRow);
  stg_CashGamesSelectCell(self, 0, SelRow, CanSelect);
  self.Refresh;
end;

procedure TfrmCageCashGame.stg_CashGamesDblClick(Sender: TObject);
var
  SelRowTou: Integer;
begin
  if stg_CashGames.Cells[0, stg_CashGames.Row] <> '' then
  begin
    frm_SellTicket.ShowSellTicketForm('', '', '', '', stg_CashGames.Cells[0, stg_CashGames.Row]);
    SelRowTou := stg_CashGames.Row;
    frmMain.GetGridData(stg_CashGames, SQL_CageCashGame, false);
    frmMain.GetGridData(stg_Players, SQL_CashGames_Players + stg_CashGames.Cells[0, stg_CashGames.Row], TRUE);
    frmMain.SelectGridRow(stg_CashGames, SelRowTou);
    stg_CashGamesSelectCell(self, 0, SelRowTou, CanSelect);
  end;
end;

procedure TfrmCageCashGame.stg_CashGamesDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  frmMain.GridDrawCell(Sender, ACol, ARow, Rect, State)
end;

procedure TfrmCageCashGame.stg_CashGamesSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
var
  WhereCond: string;
begin
  WhereCond := stg_CashGames.Cells[0, ARow];
  if (ARow > 1) then
  begin
    LastCashGameSelRow := ARow;
    if WhereCond = '' then
    begin
      WhereCond := '-1';
    end;
    lbl_CashGameName.Caption := _('Cash Game:') + ' ' + stg_CashGames.Cells[2, ARow];
    frmMain.GetGridData(stg_Players, SQL_CashGames_Players + WhereCond + ' order by WIN NULLS FIRST', false);
    stg_Players.Cells[1, 1] := _('Players') + #13#10 + '(' + IntToStr(stg_Players.RowCount - 2) + ')';
    // stg_Players.Cells[2, 1] := _('Seats') + #13#10 + '(' + IntToStr(stg_Players.RowCount-2) + ')';
    stg_Players.ColWidths[1] := 150;
    stg_Players.ColWidths[2] := 40;
    stg_Players.ColWidths[3] := 70;
    stg_Players.ColWidths[4] := 70; // input value
    stg_Players.ColWidths[5] := 70; // rebuy value
    stg_Players.ColWidths[6] := 70; // output value
    stg_Players.ColWidths[7] := 122;
    stg_Players.ColWidths[8] := -1; // do rebuy id
    stg_Players.ColWidths[9] := -1; // PLAYER ID
    stg_Players.ColWidths[10] := -1; // REBUY ACCEPT DATE
    stg_Players.ColWidths[11] := 122; //OUT DATE TIME
    stg_Players.ColWidths[12] := 40; // PPOINTS
    stg_PlayersSelectCell(self, 0, 2, CanSelect);
  end;
end;

procedure TfrmCageCashGame.stg_PlayersDblClick(Sender: TObject);
var
  BuyValue: string;
begin
  if (stg_CashGames.Row > 1) and (stg_Players.Cells[8, stg_Players.Row] <> '') and (stg_Players.Cells[10, stg_Players.Row] = '') then
  begin
    frmMain.OracleQuery1.Close;
    frmMain.OracleQuery1.SQL.Text := 'select value from CASH_GAME_TICKET_REBUYS where id=' + stg_Players.Cells[8, stg_Players.Row];
    frmMain.OracleQuery1.Execute;
    BuyValue := frmMain.OracleQuery1.Field(0);
    frm_SellTicket.ShowSellTicketForm(BuyValue, '', stg_Players.Cells[9, stg_Players.Row], '', stg_CashGames.Cells[0, stg_CashGames.Row], stg_Players.Cells[8, stg_Players.Row]);
    frmMain.btn_TicketsCashGame.Click;
  end
end;

procedure TfrmCageCashGame.stg_PlayersDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  frmMain.GridDrawCell(Sender, ACol, ARow, Rect, State)
end;

procedure TfrmCageCashGame.stg_PlayersSelectCell(Sender: TObject; ACol, ARow: Integer;

  var CanSelect: Boolean);
begin
  img_Player.Picture := nil;
  if (ARow > 1) and (stg_Players.Cells[0, ARow] <> '') then
  begin
    img_Player.Picture := uutils.GetImage('select image_SMALL, image_type from cash_game_tickets cgt join players pl on pl.id = cgt.player_id where cgt.id=' + stg_Players.Cells[0, ARow],
      frmMain.OracleSession1, TRUE);
    btn_CancelOperation.Enabled := TRUE;
    btn_PlayerOut.Enabled := TRUE;
  end
  else
  begin
    btn_CancelOperation.Enabled := false;
    btn_PlayerOut.Enabled := false;
  end;
end;

end.
